import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:jtsell_app/provider/cart_list_model.dart';
import 'package:jtsell_app/provider/index_page_model.dart';
import 'package:jtsell_app/provider/main_category_model.dart';
import 'package:jtsell_app/provider/user_login_model.dart';
import 'package:provider/provider.dart';
import '../config/app_config.dart';
import 'category_page.dart';
import 'home_page.dart';
import 'member_page.dart';
import 'shopping cart_page.dart';
import 'package:fluwx/fluwx.dart' as fluwx;

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> {
  ///底部导航栏选项
  List<BottomNavigationBarItem> bottomBarItems = [
    BottomNavigationBarItem(title: Text("首页"), icon: Icon(Icons.home)),
    BottomNavigationBarItem(title: Text("分类"), icon: Icon(Icons.search)),
    BottomNavigationBarItem(title: Text("购物车"), icon: Icon(Icons.add_shopping_cart)),
    BottomNavigationBarItem(title: Text("会员中心"), icon: Icon(Icons.person)),
  ];

  ///页面集合
  List<Widget> pages = [HomePage(), CategoryPage(), ShoppingCartPage(), MemberPage()];

  int currentIndex = 0;

  void _initApplication() {
    ///这个方法在一帧的最后调用，并且只调用一次。
    WidgetsBinding.instance.addPostFrameCallback((callback) {
      ///设置屏幕尺寸适配
    });
  }

  @override
  void initState() {
    super.initState();

    _initApplication();
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil(width: AppConfig.SCREEN_WIDTH, height: AppConfig.SCREEN_HEIGHT)..init(context);
    return MultiProvider(
        providers: [
          ChangeNotifierProvider<MainCategoryModel>.value(value: mainCategoryModel),
          ChangeNotifierProvider<IndexPageModel>.value(value: indexPageModel),
          ChangeNotifierProvider<CartListModel>.value(value: cartListModel),
          ChangeNotifierProvider<UserLoginModel>.value(value: userLoginModel),
        ],
        child: Consumer<IndexPageModel>(
            builder: (BuildContext context, IndexPageModel indexModel, Widget child) {
          return Scaffold(
            ///使用IndexedStack保证页面不被重新加载
            body: IndexedStack(index: indexModel.currentPageIndex, children: pages),
            bottomNavigationBar: BottomNavigationBar(
                type: BottomNavigationBarType.fixed,
                currentIndex: indexModel.currentPageIndex,
                items: bottomBarItems,
                onTap: (index) {
                  indexPageModel.setCurrentPageIndex(index);
                }),
          );
        }));
  }
}
